<template>
  <div class="playlist-container">
    <div class="top-wrap">
      <div class="img-wrap">
        <img :src="playLists.coverImgUrl" alt="" />
      </div>
      <div class="info-wrap">
        <p class="title">{{playLists.name}}</p>
        <div class="author-wrap">
          <img class="avatar" :src="creator.avatarUrl" alt="" />
          <span class="name">{{creator.nickname}}</span>
          <span class="time">{{playLists.createTime | timeFormat}} 创建</span>
        </div>
        <div class="play-wrap">
          <span class="iconfont icon-circle-play"></span>
          <span class="text">播放全部</span>
        </div>
        <div class="tag-wrap">
          <span class="title">标签:</span>
          <ul>
            <li v-for="(item,i) in playLists.tags" :key="i">{{item}} </li>
          </ul>
        </div>
        <div class="desc-wrap">
          <span class="title">简介:</span>
          <span class="desc">{{playLists.description}}</span>
        </div>
      </div>
    </div>
    <el-tabs v-model="activeIndex">
      <el-tab-pane label="歌曲列表" name="1">
        <table class="el-table playlit-table">
          <thead>
            <th></th>
            <th></th>
            <th>音乐标题</th>
            <th>歌手</th>
            <th>专辑</th>
            <th>时长</th>
          </thead>
          <tbody>
            <tr class="el-table__row" v-for="(item,i) in playLists.tracks" :key="i" @click="playMusic(item.id)">
              <td>{{i+1}}</td>
              <td>
                <div class="img-wrap">
                  <img :src="item.al.picUrl" alt="" />
                  <span class="iconfont icon-play"></span>
                </div>
              </td>
              <td>
                <div class="song-wrap">
                  <div class="name-wrap">
                    <span>{{item.name}}</span>
                    <span class="iconfont icon-mv"></span>
                  </div>
                </div>
              </td>
              <td>{{item.ar[0].name}}</td>
              <td>{{item.al.name}}</td>
              <td>{{item.dt | dateFormat}}</td>
            </tr>
          </tbody>
        </table>
      </el-tab-pane>
      <el-tab-pane :label="`评论(${total})`" name="2">
        <!-- 精彩/热门评论 -->
        <div class="comment-wrap">
          <p class="title">精彩评论<span class="number">({{hotTotal}})</span></p>
          <div class="comments-wrap">
            <div class="item" v-for="(item,i) in hotComment" :key="i">
              <div class="icon-wrap">
                <img :src="item.user.avatarUrl" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{item.user.nickname}}：</span>
                  <span class="comment">{{item.content}}</span>
                </div>
                <div class="re-content" v-if="item.beReplied.length != 0">
                  <span class="name">{{item.beReplied[0].user.nickname}}：</span>
                  <span class="comment">{{item.beReplied[0].content}}</span>
                </div>
                <div class="date">{{item.timeStr}}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 最新/全部评论 -->
        <div class="comment-wrap">
          <p class="title">最新评论<span class="number">({{total}})</span></p>
          <div class="comments-wrap">
            <div class="item" v-for="(item,i) in newComment" :key="i">
              <div class="icon-wrap">
                <img :src="item.user.avatarUrl" alt="" />
              </div>
              <div class="content-wrap">
                <div class="content">
                  <span class="name">{{item.user.nickname}}：</span>
                  <span class="comment">{{item.content}}</span>
                </div>
                <div class="re-content" v-if="item.beReplied.length != 0">
                  <span class="name">{{item.beReplied[0].user.nickname}}：</span>
                  <span class="comment">{{item.beReplied[0].content}}</span>
                </div>
                <div class="date">{{item.timeStr}}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 分页器 -->
        <el-pagination @current-change="handleCurrentChange" background layout="prev, pager, next" :total="total"
          :current-page="page" :page-size="5">
        </el-pagination>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
  import { playlistDetailAPI, getSongUrlAPI, getCommentAPI } from '@/api'
  export default {
    name: 'playlist',
    data() {
      return {
        activeIndex: '1',
        // 总条数
        total: 0,
        // 页码
        page: 1,
        playLists: {},
        creator: {},
        arrId: [],
        url: '',
        // 全部评论
        newComment: [],
        // 精彩/热门评论
        hotComment: [],
        hotTotal: 0
      };
    },
    created() {
      this.getPlayList(this.$route.query.q)
      this.getNewComment(this.$route.query.q)
    },
    methods: {
      handleCurrentChange(val) {
        this.page = val
        this.getNewComment(this.$route.query.q)
      },
      async getPlayList(id) {
        const res = await playlistDetailAPI(id)
        this.playLists = res.data.playlist
        this.creator = res.data.playlist.creator
        this.arrId = res.data.playlist.tracks
      },
      async playMusic(id) {
        const res = await getSongUrlAPI(id)
        this.url = res.data.data[0].url
        this.$parent.audio.url = res.data.data[0].url
        if (this.url != null) {
          this.$emit('newsInfo', id);
        } else {
          this.$message.warning('抱歉，无法播放VIP歌曲')
        }
      },
      async getNewComment(id) {
        const res = await getCommentAPI({
          id,
          offset: (this.page - 1) * 20
        })
        this.total = res.data.total
        this.newComment = res.data.comments
        this.hotComment = res.data.hotComments
        this.hotTotal = res.data.hotComments.length
      }
    }
  };
</script>

<style>

</style>